﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace GoobizFrame.Windows.Controls.XDateNavigator
{
    public partial class XDateNavigator : DevExpress.XtraEditors.XtraUserControl
    {
        private DateTime _DateTime = DateTime.Now;
        private DateTime _DateTime_Show = DateTime.Now;
        System.Collections.ArrayList arr_XDateButton = new System.Collections.ArrayList();
        GoobizFrame.Profile.Config config = new GoobizFrame.Profile.Config(
            GoobizFrame.Windows.MdiUtils.CultureInfoHelper.CurrentCultureInfoPath + @"\Lang.GoobizFrame.Windows.config");


        [Category("XDateNavigator")]
        public DateTime DateTime
        {
            get { return _DateTime; }
            set
            {
                _DateTime = value;
                _DateTime_Show = value;

                DisplayDateTimeName();
                DisplayTalbleOfDate();
            }
        }

        [Category("XDateNavigator")]
        public event XDateButtonEventHandler ButtonClick;

        /// <summary>
        /// 
        /// </summary>
        void ResetTalbleOfDate()
        {
            foreach (object objXDateButton in arr_XDateButton)
            {
                if (((XDateButton)objXDateButton).DateTime.Month != this._DateTime_Show.Month)
                    ((XDateButton)objXDateButton).ForeColor = Color.Gray;
            }

            this.Refresh();
        }
        
        /// <summary>
        /// 
        /// </summary>
        void InitArrOfXDateButton()
        {
            arr_XDateButton = new System.Collections.ArrayList();
            arr_XDateButton.AddRange(new object[]
                {   xdbSaturday6,
                    xdbFriday6,
                    xdbThursday6,
                    xdbWednesday6,
                    xdbTuesday6,
                    xdbMonday6,
                    xdbSunday6,

                    xdbSaturday5,
                    xdbFriday5,
                    xdbThursday5,
                    xdbWednesday5,
                    xdbTuesday5,
                    xdbMonday5,
                    xdbSunday5,

                    xdbSaturday4,
                    xdbFriday4,
                    xdbThursday4,
                    xdbWednesday4,
                    xdbTuesday4,
                    xdbMonday4,
                    xdbSunday4,

                    xdbSaturday3,
                    xdbFriday3,
                    xdbThursday3,
                    xdbWednesday3,
                    xdbTuesday3,
                    xdbMonday3,
                    xdbSunday3,

                    xdbSaturday2,
                    xdbFriday2,
                    xdbThursday2,
                    xdbWednesday2,
                    xdbTuesday2,
                    xdbMonday2,
                    xdbSunday2,

                    xdbSaturday1,
                    xdbFriday1,
                    xdbThursday1,
                    xdbWednesday1,
                    xdbTuesday1,
                    xdbMonday1,
                    xdbSunday1
                            });
        }

        /// <summary>
        /// set lblMonth.Text
        /// </summary>
        void DisplayDateTimeName()
        {
            Months _month = (Months)_DateTime_Show.Month;
            string _name_of_month = _month.ToString();
            lblMonth.Text = _name_of_month;
            lblYear.Text = _DateTime_Show.Year.ToString();
            try
            {
                config.GroupName = "XDateNavigator";
                string _config_name = "" + config.GetValue("Months", _name_of_month);
                if (_config_name != "")
                    lblMonth.Text = _config_name;
            }
            catch { }

            SetHeaderSize();
        }

        /// <summary>
        /// set text of XDateButton
        /// </summary>
        void DisplayTalbleOfDate()
        {
            DateTime _firstDay = new DateTime(_DateTime_Show.Year, _DateTime_Show.Month, 1);
            switch (_firstDay.DayOfWeek)
            {
                case DayOfWeek.Sunday:
                    int fact = 0;
                    xdbSunday1.DateTime = _firstDay.AddDays(fact++);
                    xdbMonday1.DateTime = _firstDay.AddDays(fact++);
                    xdbTuesday1.DateTime = _firstDay.AddDays(fact++);
                    xdbWednesday1.DateTime = _firstDay.AddDays(fact++);
                    xdbThursday1.DateTime = _firstDay.AddDays(fact++);
                    xdbFriday1.DateTime = _firstDay.AddDays(fact++);
                    xdbSaturday1.DateTime = _firstDay.AddDays(fact++);
                    break;

                case DayOfWeek.Monday:
                    fact = -1;
                    xdbSunday1.DateTime = _firstDay.AddDays(fact++);
                    xdbMonday1.DateTime = _firstDay.AddDays(fact++);
                    xdbTuesday1.DateTime = _firstDay.AddDays(fact++);
                    xdbWednesday1.DateTime = _firstDay.AddDays(fact++);
                    xdbThursday1.DateTime = _firstDay.AddDays(fact++);
                    xdbFriday1.DateTime = _firstDay.AddDays(fact++);
                    xdbSaturday1.DateTime = _firstDay.AddDays(fact++);
                    break;
                case DayOfWeek.Tuesday:
                    fact = -2;
                    xdbSunday1.DateTime = _firstDay.AddDays(fact++);
                    xdbMonday1.DateTime = _firstDay.AddDays(fact++);
                    xdbTuesday1.DateTime = _firstDay.AddDays(fact++);
                    xdbWednesday1.DateTime = _firstDay.AddDays(fact++);
                    xdbThursday1.DateTime = _firstDay.AddDays(fact++);
                    xdbFriday1.DateTime = _firstDay.AddDays(fact++);
                    xdbSaturday1.DateTime = _firstDay.AddDays(fact++);
                    break;
                case DayOfWeek.Wednesday:
                    fact = -3;
                    xdbSunday1.DateTime = _firstDay.AddDays(fact++);
                    xdbMonday1.DateTime = _firstDay.AddDays(fact++);
                    xdbTuesday1.DateTime = _firstDay.AddDays(fact++);
                    xdbWednesday1.DateTime = _firstDay.AddDays(fact++);
                    xdbThursday1.DateTime = _firstDay.AddDays(fact++);
                    xdbFriday1.DateTime = _firstDay.AddDays(fact++);
                    xdbSaturday1.DateTime = _firstDay.AddDays(fact++);
                    break;
                case DayOfWeek.Thursday:
                    fact = -4;
                    xdbSunday1.DateTime = _firstDay.AddDays(fact++);
                    xdbMonday1.DateTime = _firstDay.AddDays(fact++);
                    xdbTuesday1.DateTime = _firstDay.AddDays(fact++);
                    xdbWednesday1.DateTime = _firstDay.AddDays(fact++);
                    xdbThursday1.DateTime = _firstDay.AddDays(fact++);
                    xdbFriday1.DateTime = _firstDay.AddDays(fact++);
                    xdbSaturday1.DateTime = _firstDay.AddDays(fact++);
                    break;
                case DayOfWeek.Friday:
                    fact = -5;
                    xdbSunday1.DateTime = _firstDay.AddDays(fact++);
                    xdbMonday1.DateTime = _firstDay.AddDays(fact++);
                    xdbTuesday1.DateTime = _firstDay.AddDays(fact++);
                    xdbWednesday1.DateTime = _firstDay.AddDays(fact++);
                    xdbThursday1.DateTime = _firstDay.AddDays(fact++);
                    xdbFriday1.DateTime = _firstDay.AddDays(fact++);
                    xdbSaturday1.DateTime = _firstDay.AddDays(fact++);
                    break;
                case DayOfWeek.Saturday:
                    fact = -6;
                    xdbSunday1.DateTime = _firstDay.AddDays(fact++);
                    xdbMonday1.DateTime = _firstDay.AddDays(fact++);
                    xdbTuesday1.DateTime = _firstDay.AddDays(fact++);
                    xdbWednesday1.DateTime = _firstDay.AddDays(fact++);
                    xdbThursday1.DateTime = _firstDay.AddDays(fact++);
                    xdbFriday1.DateTime = _firstDay.AddDays(fact++);
                    xdbSaturday1.DateTime = _firstDay.AddDays(fact++);
                    break;
            }

            xdbSunday2.DateTime = xdbSunday1.DateTime.AddDays(7);
            xdbSunday3.DateTime = xdbSunday2.DateTime.AddDays(7);
            xdbSunday4.DateTime = xdbSunday3.DateTime.AddDays(7);
            xdbSunday5.DateTime = xdbSunday4.DateTime.AddDays(7);
            xdbSunday6.DateTime = xdbSunday5.DateTime.AddDays(7);

            xdbMonday2.DateTime = xdbMonday1.DateTime.AddDays(7);
            xdbMonday3.DateTime = xdbMonday2.DateTime.AddDays(7);
            xdbMonday4.DateTime = xdbMonday3.DateTime.AddDays(7);
            xdbMonday5.DateTime = xdbMonday4.DateTime.AddDays(7);
            xdbMonday6.DateTime = xdbMonday5.DateTime.AddDays(7);

            xdbTuesday2.DateTime = xdbTuesday1.DateTime.AddDays(7);
            xdbTuesday3.DateTime = xdbTuesday2.DateTime.AddDays(7);
            xdbTuesday4.DateTime = xdbTuesday3.DateTime.AddDays(7);
            xdbTuesday5.DateTime = xdbTuesday4.DateTime.AddDays(7);
            xdbTuesday6.DateTime = xdbTuesday5.DateTime.AddDays(7);

            xdbWednesday2.DateTime = xdbWednesday1.DateTime.AddDays(7);
            xdbWednesday3.DateTime = xdbWednesday2.DateTime.AddDays(7);
            xdbWednesday4.DateTime = xdbWednesday3.DateTime.AddDays(7);
            xdbWednesday5.DateTime = xdbWednesday4.DateTime.AddDays(7);
            xdbWednesday6.DateTime = xdbWednesday5.DateTime.AddDays(7);

            xdbThursday2.DateTime = xdbThursday1.DateTime.AddDays(7);
            xdbThursday3.DateTime = xdbThursday2.DateTime.AddDays(7);
            xdbThursday4.DateTime = xdbThursday3.DateTime.AddDays(7);
            xdbThursday5.DateTime = xdbThursday4.DateTime.AddDays(7);
            xdbThursday6.DateTime = xdbThursday5.DateTime.AddDays(7);

            xdbFriday2.DateTime = xdbFriday1.DateTime.AddDays(7);
            xdbFriday3.DateTime = xdbFriday2.DateTime.AddDays(7);
            xdbFriday4.DateTime = xdbFriday3.DateTime.AddDays(7);
            xdbFriday5.DateTime = xdbFriday4.DateTime.AddDays(7);
            xdbFriday6.DateTime = xdbFriday5.DateTime.AddDays(7);

            xdbSaturday2.DateTime = xdbSaturday1.DateTime.AddDays(7);
            xdbSaturday3.DateTime = xdbSaturday2.DateTime.AddDays(7);
            xdbSaturday4.DateTime = xdbSaturday3.DateTime.AddDays(7);
            xdbSaturday5.DateTime = xdbSaturday4.DateTime.AddDays(7);
            xdbSaturday6.DateTime = xdbSaturday5.DateTime.AddDays(7);

            ResetTalbleOfDate();
        }

        /// <summary>
        /// 
        /// </summary>
        public XDateNavigator()
        {
            InitializeComponent();

            InitArrOfXDateButton();
            DisplayDateTimeName();
            SetDayOfWeek();
            DisplayTalbleOfDate();

            SetTableSize();
        }

        /// <summary>
        /// 
        /// </summary>
        public enum Months
        {
            January = 1,
            February = 2,
            March = 3,
            April = 4,
            May = 5,
            June = 6,
            July = 7,
            August = 8,
            September = 9,
            October = 10,
            November = 11,
            December = 12
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMonthPrev_Click(object sender, EventArgs e)
        {
            _DateTime_Show = _DateTime_Show.AddMonths(-1);
            DisplayDateTimeName();
            DisplayTalbleOfDate();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMonthNext_Click(object sender, EventArgs e)
        {
            _DateTime_Show = _DateTime_Show.AddMonths(1);
            DisplayDateTimeName();
            DisplayTalbleOfDate();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnYearPrev_Click(object sender, EventArgs e)
        {
            _DateTime_Show = _DateTime_Show.AddYears(-1);
            DisplayDateTimeName();
            DisplayTalbleOfDate();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnYearNext_Click(object sender, EventArgs e)
        {
            _DateTime_Show = _DateTime_Show.AddYears(1);
            DisplayDateTimeName();
            DisplayTalbleOfDate();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xdbSunday1_XDateButtonClick(object sender, XDateButtonEventArgs e)
        {
            this._DateTime = e.DateTime;
            if (_DateTime.Month != _DateTime_Show.Month)
            {
                _DateTime_Show = _DateTime;
                DisplayDateTimeName();
                DisplayTalbleOfDate();
            }

            if (ButtonClick != null)
                ButtonClick(sender, e);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnToday_Click(object sender, EventArgs e)
        {
            if (_DateTime_Show.ToShortDateString() != DateTime.Now.ToShortDateString())
            {
                this.DateTime = DateTime.Now;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void XDateNavigator_SizeChanged(object sender, EventArgs e)
        {
            SetTableSize();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panelControl1_SizeChanged(object sender, EventArgs e)
        {
            SetHeaderSize();

        }

        /// <summary>
        /// 
        /// </summary>
        void SetTableSize()
        {
            tableLayoutPanel1.Size = new Size(
                           (int)(this.Width / 7) * 7,
                           (int)(this.Height / 8) * 8
                           );
            for (int i_col = 0; i_col < tableLayoutPanel1.ColumnCount; i_col++)
            {
                tableLayoutPanel1.ColumnStyles[i_col].SizeType = SizeType.Absolute;
                tableLayoutPanel1.ColumnStyles[i_col].Width = this.Width / tableLayoutPanel1.ColumnCount;
            }

            for (int i_row = 0; i_row < tableLayoutPanel1.RowCount; i_row++)
            {
                tableLayoutPanel1.RowStyles[i_row].SizeType = SizeType.Absolute;
                tableLayoutPanel1.RowStyles[i_row].Height = this.Height / tableLayoutPanel1.RowCount;
            }
            this.Refresh();
        }

        /// <summary>
        /// 
        /// </summary>
        void SetHeaderSize()
        {
            btnMonthNext.Width = panelHeader.Height;
            btnMonthPrev.Width = panelHeader.Height;

            btnYearNext.Width = panelHeader.Height;
            btnYearPrev.Width = panelHeader.Height;

            lblMonth.Width = MdiUtils.SizeHelper.GetTextSizeInPixel(lblMonth.Text, this.CreateGraphics()).Width;
            lblYear.Width = MdiUtils.SizeHelper.GetTextSizeInPixel(lblYear.Text, this.CreateGraphics()).Width;
        }

        /// <summary>
        /// 
        /// </summary>
        void SetDayOfWeek()
        {
            try
            {
                config.GroupName = "XDateNavigator";
                if (config.GetEntryNames("DayOfWeek").Length != 0)
                {
                    lblSunday.Text = "" + config.GetValue("DayOfWeek", "Sunday");
                    lblMonday.Text = "" + config.GetValue("DayOfWeek", "Monday");
                    lblTuesday.Text = "" + config.GetValue("DayOfWeek", "Tuesday");
                    lblWednesday.Text = "" + config.GetValue("DayOfWeek", "Wednesday");
                    lblThursday.Text = "" + config.GetValue("DayOfWeek", "Thursday");
                    lblFriday.Text = "" + config.GetValue("DayOfWeek", "Friday");
                    lblSaturday.Text = "" + config.GetValue("DayOfWeek", "Saturday");

                    btnToday.Text = "" + config.GetValue("DayOfWeek", "Today");
                }
            }
            catch (Exception ex)
            { }
        }
    }
}
